package com.GainLoop.Api;

import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;
import org.json.JSONArray;
import org.json.JSONException;

import com.GainLoop.Api.Entity.ArrayEntity;
import com.GainLoop.Api.Entity.EntityAbstract;
import com.GainLoop.Api.Entity.Gain;
import com.GainLoop.Api.Entity.Tag;

public class Gains extends ApiAbstract {


	public ApiResponseObject<Gain> create(Gain gain, ArrayList<Tag> tags) throws JSONException, JSONRPCException
	{
		if(gain.getId() != 0) {
			throw new ApiException("gain id must not set");
		}
		
		JSONArray tagsIds = new JSONArray();
		for(Tag tag : tags) {
			tagsIds.put(tag.getId());
		}
		 
		return new ApiResponseObject<Gain>(GetClient().callJSONObject("gains.create", gain.toObject(), tagsIds), Gain.class);
	}
	
	public ApiResponseObject<Gain> getById(long id) throws JSONException, JSONRPCException
	{
		return new ApiResponseObject<Gain>(GetClient().callJSONObject("gains.getById", id), Gain.class);
	}
	
	public ApiResponseArray<Gain> getWishes(int page) throws JSONException, JSONRPCException
	{
		return getWishes(page, 10);
	}
	
	public ApiResponseArray<Gain> getWishes(int page, int pageLimit) throws JSONException, JSONRPCException
	{
		if(pageLimit < 1 || pageLimit > 25) {
			throw new ApiException("invalid pageLimit");
		}
		if(page < 1 || page > 50) {
			throw new ApiException("invalid page");
		}
		
		return new ApiResponseArray<Gain>(GetClient().callJSONObject("gains.getWishes", page, pageLimit), Gain.class);
	}

	/**
	 * @deprecated 
	 */
	public ApiResponseArray<Gain> getOffers(int page) throws JSONException, JSONRPCException
	{
		return getOffers(page, 10);
	}
	
	/**
	 * @deprecated 
	 */
	public ApiResponseArray<Gain> getOffers(int page, int pageLimit) throws JSONException, JSONRPCException
	{
		if(pageLimit < 1 || pageLimit > 25) {
			throw new ApiException("invalid pageLimit");
		}
		if(page < 1 || page > 50) {
			throw new ApiException("invalid page");
		}
		
		return new ApiResponseArray<Gain>(GetClient().callJSONObject("gains.getWishes", page, pageLimit), Gain.class);
	}
	
}
